﻿using sqlsearcher.Models;
using sqlsearcher.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace sqlsearcher
{
    /// <summary>
    /// 数据库表字段搜索类
    /// </summary>
    public class CNSqlSearch
    {
        public string CurrentDB;
        public Dictionary<string, string> serachKeywords = new Dictionary<string, string>();

        internal string GetTableDescSQL()
        {
            return TFac<SearchManager>.Instance.GetTableDescSQL();
        }

        public void Fill(DataTable dt)
        {
            this.serachKeywords = new Dictionary<string, string>();
            var searchkeyWords = dt.ToList<CNEntity>();

            var tcSearchKeyWords = searchkeyWords;
            if(tcSearchKeyWords!=null)
            {
                foreach (var item in tcSearchKeyWords)
                {
                    if(!this.serachKeywords.ContainsKey(item.CName))
                    this.serachKeywords.Add(item.CName, item.CDesc);
                }
            }
            var tbSearchKeyWords = searchkeyWords;
            if (tbSearchKeyWords != null)
            {
                foreach (var item in tbSearchKeyWords)
                {
                    if (!this.serachKeywords.ContainsKey(item.TbName))
                        this.serachKeywords.Add(item.TbName,item.TbDesc);
                }
            }

        }

        public Dictionary<string, string> Search(string indexName)
        {
            if (string.IsNullOrWhiteSpace(indexName))
                return new Dictionary<string, string>();

            var res = new Dictionary<string, string>();
            if (serachKeywords != null && serachKeywords.Count > 0)
            {
                foreach (var m in serachKeywords)
                {
                    if (m.Key != null && m.Key.ToLower().IndexOf(indexName.ToLower()) != -1 ||
                       m.Value != null && m.Value.ToLower().IndexOf(indexName) != -1)
                    {
                        res.Add(m.Key, m.Value);
                    }
                }
            }

            return res;
        }

        public void Demo()
        {
            //var keywords = "日期";
            //var sqlCnSearch = new CNSqlSearch();
            //sqlCnSearch.Fill(getConn(sqlCnSearch.GetTableDescSQL()));
            //var res = sqlCnSearch.Search(keywords);
            //if (res != null)
            //{
            //    foreach (var item in res.Keys)
            //    {
            //        Console.WriteLine($"词={item},值={res[item]}");
            //    }
            //}
        }

        private class CNEntity
        {
            public string TbName { get; set; }
            public string TbDesc { get; set; }
            public string CName { get; set; }
            public string CDesc { get; set; }
        }
    }
}
